package com.jcfk.eam.controller.mould;

import com.alibaba.excel.EasyExcel;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.controller.parent.EamAssetModelController;
import com.jcfk.eam.domain.dto.base.EamAssetModelDTO;
import com.jcfk.eam.domain.query.base.EamAssetModelQuery;
import com.jcfk.eam.domain.uploadExcel.base.EamAssetModelUploadExcel;
import com.jcfk.eam.enums.AssetKind;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 模具型号 服务接口
 *
 * @author yql
 * @email 522891237@qq.com
 * @date 2024-07-24
 */
@Api(tags = {"模具型号管理"})
@RestController
@RequestMapping("mouldAssetModel")
public class MouldAssetModelController {
    @Autowired
    private EamAssetModelController eamAssetModelController;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetModelQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具型号")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetModelDTO> queryByPage(@RequestBody ReqPageParam<EamAssetModelQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.MOULD.getValue());
        return eamAssetModelController.queryByPage(requestParam);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamAssetBrandQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具型号列表", notes = "获取模具型号列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamAssetModelDTO>> getList(@RequestBody EamAssetModelQuery query) {
        query.setAssetKind(AssetKind.MOULD.getValue());
        return eamAssetModelController.getList(query);
    }

    /**
     *  模具型号
     * 根据id 查询详情
     */
    @ApiOperation("查询模具型号")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetModelDTO> get(@PathVariable("id") String id) {
        return eamAssetModelController.get(id);
    }

    /**
     * 添加/更新管理模具型号
     */
    @ApiOperation("保存模具型号")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具型号管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetModelDTO eamAssetModel) {
        eamAssetModel.setAssetKind(AssetKind.MOULD.getValue());
        return eamAssetModelController.save(eamAssetModel);
    }

    /**
     *  批量删除模具型号
     */
    @ApiOperation("批量删除模具型号")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具型号管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetModelDTO eamAssetModel) {
        return eamAssetModelController.delete(eamAssetModel);
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetModelQuery requestParam, HttpServletResponse response) throws Exception {
        requestParam.setAssetKind(AssetKind.MOULD.getValue());
        eamAssetModelController.export(requestParam,response);
    }

    /**
     * 导入模具型号
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamAssetModelUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetModelUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        items.forEach(e -> e.setAssetKind(AssetKind.MOULD.getValue()));
        eamAssetModelController.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        eamAssetModelController.exportTemp(response);
    }

    /**
     *  删除模具型号
     */
    @ApiOperation("删除模具型号")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具型号管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        return eamAssetModelController.deleteItem(id);
    }
}
